<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12/012
 * Time: 11:57
 */

namespace app\api\service;


use app\lib\exception\OrderException;
use app\api\model\OrderBuy as OrderBuyModel;
use app\api\model\OrderBuyAfterSale as OrderBuyAfterSaleModel;
use app\api\service\Order as OrderService;
use app\api\model\OrderBuyAfterSaleImg as OrderBuyAfterSaleImgModel;
use think\Db;
use think\Exception;

class OrderBuyAfterSale {

    /**
     * 创建商城订单售后单
     * @param $params
     * @return bool
     * @throws Exception
     * @throws OrderException
     */
    public static function createAftermarket($params) {

        $uid = Token::getCurrentUid();

        $order_buy_after_sale = OrderBuyAfterSaleModel::get(function($query) use ($params) {
            $query->where([['order_buy_id', '=', $params['order_id']], ['status', 'lt', 7]]);
        });

        if ($order_buy_after_sale) {
            throw new OrderException([
                'msg' => '请勿重复申请售后',
                'errorCode' => 60006
            ]);
        }

        // 获取订单数据
        $order = OrderBuyModel::get(['id' => $params['order_id'], 'member_id' => $uid]);
        if (!$order) {
            throw new OrderException();
        }

        // 检查订单数据
        if (($order['status'] != 3 && $order['status'] != 99) || ($order['after_sale_status'] != 0 && $order['after_sale_status'] != 3)) {
            throw new OrderException([
                'msg' => '订单状态异常',
                'errorCode' => 60002
            ]);
        }

        $aftermarket_sn = OrderService::makeOrderNo('XSSH');
        // 添加售后记录
        $aftermarket_data = [
            'aftermarket_sn' => $aftermarket_sn,
            'member_id' => $order['member_id'],
            'msg' => $params['msg'],
            'linkman' => $params['linkman'],
            'mobile' => $params['mobile'],
            'order_buy_id' => $params['order_id'],
            'order_status' => $order['status'],
            'type' => $params['type']
        ];

        Db::startTrans();
        try {
            $order_buy_after_sale = OrderBuyAfterSaleModel::create($aftermarket_data);

            // 保存售后图片
            $aftermarket_img_data = [];
            if (isset($params['image']) && is_array($params['image'])) {
                foreach ($params['image'] as $v) {
                    $aftermarket_img_data[] = [
                        'order_buy_after_sale_id' => $order_buy_after_sale->id,
                        'image' => $v
                    ];
                }
                (new OrderBuyAfterSaleImgModel)->saveAll($aftermarket_img_data);
//                OrderBuyAfterSaleImgModel::saveAftermarketImg($aftermarket_img_data);
            }

            // 修改订单售后状态
            $order->save(['after_sale_status' => 1], ['id' => $params['order_id']]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }

        // 短信通知
        OrderService::sendAfterSaleNote($aftermarket_sn);

        return true;
    }
}